package cj.web.tb.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.login.controllers.BaseController;
import cj.web.tb.constants.SchoolTypeEnum;
import cj.web.tb.dao.TBAreaDAO;
import cj.web.tb.dao.TBSchoolDAO;
import cj.web.tb.domain.TBSchool;

@Path("/tBSchool")
public class TBSchoolController extends BaseController{

	private static Logger logger = Logger.getLogger(TBSchoolController.class.getName());
	
	@Autowired
	private TBSchoolDAO tBSchoolDao;
	@Autowired
	private TBAreaDAO tBAreaDao;
	
	@Get("show")
	public String show(Model model,TBSchool tBSchool) {
		List<String> areaNameLis = tBAreaDao.queryAreaNameAll();
		
		model.add("arealis", areaNameLis);
		return "/views/tb/schoolList.vm";
	}

	@Post("getList")
	public String queryList(TBSchool tBSchool) {
		//当前页  
		tBSchool.setStart((tBSchool.getPage()-1)*tBSchool.getRows());
		tBSchool.setLength(tBSchool.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<TBSchool> objs = tBSchoolDao.query(tBSchool);
		
		int size = tBSchoolDao.querySize(tBSchool);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model) {
		
		List<String> areaNameLis = tBAreaDao.queryAreaNameAll();
		
		model.add("arealis", areaNameLis);
		model.add("schoolType",EnumSet.allOf(SchoolTypeEnum.class));
		return "/views/tb/schoolDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,TBSchool tBSchool,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBSchool.setCreateUser(returnSessionObj(request).getAccount());
			tBSchool.setUpdateUser(returnSessionObj(request).getAccount());
			tBSchoolDao.create(tBSchool);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) {
		TBSchool obj = tBSchoolDao.queryById(id);
		List<String> areaNameLis = tBAreaDao.queryAreaNameAll();
		model.add("obj", obj);
		model.add("arealis", areaNameLis);
		model.add("schoolType",EnumSet.allOf(SchoolTypeEnum.class));
		return "/views/tb/schoolEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,TBSchool tBSchool,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBSchool.setUpdateUser(returnSessionObj(request).getAccount());
			tBSchoolDao.update(tBSchool);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			tBSchoolDao.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}